#!/bin/bash

#upload war file to remote server
echo input params: $*
remote_name=(server1 server2 server3)

declare -A remote_array
declare -A war_array

remote_array=([server1]="10.193.78.31" [server2]="10.193.78.34" [server3]="10.193.78.35")
war_array=([server1]="payweb meradmin" [server2]="userCenter merchants payment" [server3]="directLine inmanage")


function usage()
{
  echo "==================================================================================="
  echo "Usage:deploy.sh"
  echo -e "\t-d : [1 2 3 a] \e[1;31m required.\e[0m desc: exec remote shell cmd for redeploy weblogic "
  echo -e "\t\t////////////////////////////////////////////////////////"
  echo -e "\t\t\e[1;31m1 :server1 ; 2: server2 ; 3 :server3 ; a :all server \e[0m"
  for i in `eval echo {0..$((${#remote_name[*]}-1))}`
  do
    echo -e "\t\t${remote_name[i]}:${remote_array["${remote_name[i]}"]}"
  done

  echo -e "\t\t///////////////////////////////////////////////////////"
  echo -e "\t-r : \e[1;31m not required.\e[0m desc:remove war file,default is don't remove war file"

  echo "**********************************************"
  echo -e "example: ./deploy.sh -rd \"1 2 3\""
  echo -e "\tdesc: exec server1,server2,server3 shell cmd."
  echo "**********************************************"
  echo -e "example: ./deploy.sh -rd \"a\""
  echo -e "\tdesc: exec all server shell cmd."
  echo "**********************************************"
  echo -e "example: ./deploy.sh -d \"a\""
  echo -e "\tdesc: don\'t remove war file."
  echo "**********************************************"

  echo "===================================================================================="
  exit 1
}

d="a"
r="n"

while getopts "d:r" opt
do
  case $opt in
  d)
    d=$OPTARG;;
  r)
    r="y";;
  ?)
    usage ;;
  esac

done

if [ "$d"x = "a"x ];then
  d="1 2 3"
fi

war_base_dir="/apps/wars/"
remote_user="root"
remote_war_dir="/apps/besp/"
remote_shell_cmd="/root/redeployUAT.sh"

#upload war files to remote server
for i in `eval echo {0..$((${#remote_name[*]}-1))}`
do
   ip="${remote_array["${remote_name[i]}"]}"

   echo "***********************************************************"
   echo "start server $ip"

   for war in  ${war_array["${remote_name[i]}"]}
   do
     war_file="$war_base_dir$war.war"
     if [ -f "$war_file" ]; then
       echo "$war_file is exist,start upload to $ip:$remote_war_dir"
       echo scp $war_file $remote_user@$ip:$remote_war_dir
       scp $war_file $remote_user@$ip:$remote_war_dir

       if [ "$r"x = "y"x ]; then
          echo "remove $war_file"
          rm -rf $war_file
       fi
     else
       echo $war_file is not exist, skip
     fi
   done

   #exec remote server weblogic shell
   if [[ $d =~ $((${i}+1)) ]];then 
     echo "====================================================================="
     echo "exec ssh -t  $remote_user@$ip \"$remote_shell_cmd\"" 
     ssh -t  $remote_user@$ip "$remote_shell_cmd"
     echo "====================================================================="
   fi
echo "end server $ip"
echo "***********************************************************"
done
